package com.jarvan.controller;

import com.jarvan.dto.TicketDto;
import com.jarvan.enums.ResultEnum;
import com.jarvan.enums.TicketEnum;
import com.jarvan.exception.TrainException;
import com.jarvan.form.BuyerForm;
import com.jarvan.form.TicketForm;
import com.jarvan.model.Ticket;
import com.jarvan.service.impl.TicketServiceImpl;
import com.jarvan.utils.ResultVoUtil;
import com.jarvan.vo.ResultVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import javax.validation.Valid;

@Api(value = "车票api")
@Slf4j
@RestController
@RequestMapping("/ticket")
public class TicketController {

    @Autowired
    private TicketServiceImpl ticketService;

    @PostMapping("/createTicket")
    public ResultVo<TicketDto> createTicket(@Valid TicketForm ticketForm, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            log.error("[创建订单]---参数不全  ticketForm={}", ticketForm);
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(),
                    ResultEnum.PARAM_ERROR.getMessage());
        }
        
        TicketDto dto = ticketService.save(ticketForm);
        return ResultVoUtil.success(dto);
    }

    @GetMapping("/findByBuyerId")
    public ResultVo<List<Ticket>> findByBuyerId(@RequestParam("buyerId") Integer buyerId){
        if (buyerId == null){
            log.error("[订单查询]  查询当前用户订单  未传id");
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        List<Ticket> tickets = ticketService.findAllByBuyer(buyerId);
        return ResultVoUtil.success(tickets);
    }

    @GetMapping("/findByBuyerAndStatus")
    public ResultVo<List<Ticket>> findAllByStatus(@Valid BuyerForm buyerForm, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            log.error("[车票查询] --参数不全  buyerForm={}", buyerForm);
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        List<Ticket> tickets = ticketService.findAllByBuyerAndStatus(buyerForm.getBuyerId(), buyerForm.getTicketStatus());
        return ResultVoUtil.success(tickets);
    }

    @GetMapping("/cancal")
    public ResultVo<Ticket> deleteTicket(@RequestParam("ticketId") String ticketId){
        if (ticketId.isEmpty()){
            log.error("[订单取消] 订单id未传");
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        Ticket ticket = ticketService.findById(ticketId);
        ticket.setTicketStatus(TicketEnum.CANCEL.getCode());
        ticketService.update(ticket);
        return ResultVoUtil.success();
    }

    @GetMapping("/finished")
    public ResultVo<Ticket> finishTicket(@RequestParam("ticketId") String ticketId){
        if (ticketId.isEmpty()){
            log.error("[订单完成] 订单id未传");
            throw new TrainException(ResultEnum.PARAM_ERROR.getCode(), ResultEnum.PARAM_ERROR.getMessage());
        }
        Ticket ticket = ticketService.findById(ticketId);
        ticket.setTicketStatus(TicketEnum.FINISH.getCode());
        ticketService.update(ticket);
        return ResultVoUtil.success();
    }
}
